package com.zheng.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zheng.common.Result;
import com.zheng.entity.Showroom;
import com.zheng.service.ShowroomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/showroom")
public class ShowroomController {
    @Autowired
    private ShowroomService showroomService;

    @PostMapping
    public  Result<?> save(@RequestBody Showroom showroom){
        showroomService.save(showroom);
        return Result.success();
    }

    @PutMapping
    public Result<?> updateShowroom(@RequestBody Showroom showroom){
        showroomService.updateById(showroom);
        return Result.success();
    }
    @GetMapping
    //pageNum 第几页，pageSize 每页个数
    public Result<?> selectAll(@RequestParam Integer pageNum,@RequestParam Integer pageSize,@RequestParam String search,@RequestParam String option){
        Page<Showroom> page = new Page<>(pageNum,pageSize);
        QueryWrapper<Showroom> wrapper =new QueryWrapper();
        if(StrUtil.isNotBlank(search)) {
            wrapper.like(option, search);
        }
        Page<Showroom> bookPage = showroomService.page(page, wrapper);
        return  Result.success(bookPage);
    }

    @DeleteMapping("/{id}")
    public Result<?> deleteShowroom(@PathVariable Integer id){

        showroomService.removeById(id);
        return Result.success();
    }
}
